import random
import os
import time
from tkinter import *
from tkinter import messagebox
from tkinter import Canvas
from PIL import Image, ImageTk
import pymysql
import _thread

def shitu():
    def MapCreator(m, n, MaxEcnt):
        # 从起始点开始随机行走，走到不能再走为止
        def inborder(x, y):
            return x < n and x >= 0 and y < m and y >= 0

        # 记录每个点是否经过过
        visited = [[False for i in range(m)] for j in range(n)]
        # 记录迷宫形状，注意n行,m列的迷宫应该包含2n+1行，2m+1列
        # 因为a条平行的通路之间和两侧一共还有a+1堵墙
        map = [[0 for i in range(2 * m + 1)] for j in range(2 * n + 1)]
        dx = [-1, 1, 0, 0]
        dy = [0, 0, -1, 1]
        # 分别是上下左右的位移矢量
        # 起始点位置和终止点位置
        global shitu1, shitu2
        shitu1 = []
        shitu2 = []
        startposx = 0
        startposy = 0
        endposx = m - 1
        endposy = n - 1
        # px，py是指针当前指向的位置，visited[px][py]对应于map[2px+1][2py+1]
        px = startposx
        py = startposy
        visited[px][py] = True
        visitedpos = [(px, py)]  # 之前走过的所有点构成的列表
        map[2 * px + 1][2 * py + 1] = 1
        sum = 1  # 经过点的总数，当达到mn时迷宫就构建好了
        while (sum < m * n):
            walkable = []  # 存储px，py点前后左右四个点中能到达的点对应的i
            for i in range(4):
                # 计算新的点的位置
                tx = px + dx[i]
                ty = py + dy[i]
                if inborder(tx, ty) and not visited[tx][ty]:  # 能到达就将i加入walkable
                    walkable.append(i)
            if not walkable or (px == endposx and py == endposy):
                # 如果walkable为空，或已到达终点，则向之前的位置随机传送
                px, py = random.choice(visitedpos)
            else:
                # 随机选择方向行进
                i = random.choice(walkable)
                px += dx[i]
                py += dy[i]
                print(px)
                print(py)
                print()
                visited[px][py] = True
                visitedpos.append((px, py))
                # 将到达的点和前后两点之间的墙赋值为1（路）
                map[2 * px + 1][2 * py + 1] = 1
                map[2 * px + 1 - dx[i]][2 * py + 1 - dy[i]] = 1
                shitu1.append(((2 * px + 1), (2 * py + 1)))
                shitu2.append(((2 * px + 1 - dx[i]), (2 * py + 1 - dy[i])))
                print(2 * px + 1)
                print(2 * py + 1)
                print(2 * px + 1 - dx[i])
                print(2 * py + 1 - dy[i])
                print()
                sum += 1
        #                             self.create_image(4 * n + rowIndex * 24, m * 4 + index * 24, image=wall)

        # 返回map
        global smap
        smap = []
        ecnt = 0
        for j in range(2 * n + 1):
            s = ""
            for i in range(2 * m + 1):
                r = random.random()
                if map[j][i]:
                    if (r >= 0 and r < 0.1):
                        s += 'G'
                    elif (r >= 0.1 and r < 0.15 and ecnt < MaxEcnt):
                        s += 'E'
                        ecnt += 1
                    else:
                        s += ' '
                else:
                    s += '@'
            smap.append(s)

    def back(event):
        if shitu1:
            pass
        else:
            createMz(canvas)
            return
        now = shitu1[0]
        rowIndex, index = now
        canvas.create_image(4 * 5 + rowIndex * 24, 5 * 4 + index * 24, image=pr)
        now = shitu2[0]
        rowIndex, index = now
        canvas.create_image(4 * 5 + rowIndex * 24, 5 * 4 + index * 24, image=pr)
        del shitu1[0]
        del shitu2[0]

    def createMz(self):
        for index, value in enumerate(smap):
            c = []
            i = 0
            for rowIndex, rowValue in enumerate(value):
                c.append(rowValue)
                if rowValue == '@':
                    self.create_image(4 * 5 + rowIndex * 24, 5 * 4 + index * 24, image=wall)
                elif rowValue == "E":
                    self.create_image(4 * 5 + rowIndex * 24, 5 * 4 + index * 24, image=e)
                    i += 1
                elif rowValue == "G":
                    self.create_image(4 * 5 + rowIndex * 24, 5 * 4 + index * 24, image=gold)

    root00 = Tk()
    root00.title("按任意键继续！")
    root00.geometry("1000x600+222+100")
    canvas = Canvas(root00)
    canvas.pack()
    wall = PhotoImage(file="photo/wall.gif")
    e = PhotoImage(file="photo/e.gif")
    gold = PhotoImage(file="photo/gold.gif")
    pr = PhotoImage(file="photo/pr.gif")
    pl = PhotoImage(file="photo/pl.gif")
    canvas.create_image(4 * 5 + 24, 5 * 4 + 24, image=pr)
    root00.bind("<KeyPress>", back)

    MapCreator(5, 5, 3)

    root00.mainloop()
shitu()
